package com.sky.Push;

import io.netty.channel.ChannelHandlerContext;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by sun on 2017-06-13.
 */
public class ClientManager {
    private static class ClientManagerHolder {
        private static final ClientManager INSTANCE = new ClientManager();
    }
    private ConcurrentHashMap<String,Object> clientMap;

    public ClientManager()
    {
        clientMap = new ConcurrentHashMap<String,Object>();

//        Thread t = new Thread(new PushTread());
//        t.start();
    }

    public static ClientManager getInstance()
    {
        return ClientManagerHolder.INSTANCE;
    }

    public void addClient(String key,Object obj)
    {
        clientMap.put(key,obj);
    }

    public void removeClient(String key)
    {
        clientMap.remove(key);
    }

    public void pushAllClient(Object msg)
    {
        ChannelHandlerContext ctx;

        System.out.println(clientMap.size());

        for(Object obj : clientMap.values())
        {
            ctx = (ChannelHandlerContext)obj;

            if(ctx.channel().isWritable())
            {
                ctx.writeAndFlush(msg);
            }

        }
    }

    public int getClientCount()
    {
        return clientMap.size();
    }


}
